<?php

/**
 * This is the model class for table "insurances".
 *
 * The followings are the available columns in table 'insurances':
 * @property integer $id
 * @property integer $ownerClass
 * @property integer $ownerId
 * @property string $name
 * @property string $code
 * @property string $validFrom
 * @property string $validUntil
 * @property string $note
 * @property string $createdAt
 * @property string $updatedAt
 * @property integer $createdBy
 * @property integer $updatedBy
 */
class Insurance extends ActiveRecordUser {

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Insurance the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'insurances';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('name, code', 'length', 'max'=>128),
			array(
				'validFrom, validUntil',
				'date',
				'format'=>'yyyy-MM-dd',
			),
			array('note', 'safe'),
			array(
				'ownerClass, ownerId, name, code, validFrom, validUntil, note',
				'safe',
				'on'=>'search'
			),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		return array();
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id'=>'ID',
			'ownerClass'=>'Owner',
			'owner.name'=>'Owner Name',
			'ownerId'=>'Owner',
			'name'=>'Name',
			'code'=>'Code',
			'validFrom'=>'Valid From',
			'validUntil'=>'Valid Until',
			'note'=>'Note',
			'createdAt'=>'Created At',
			'updatedAt'=>'Updated At',
			'createdBy'=>'Created By',
			'updatedBy'=>'Updated By',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		$criteria = new CDbCriteria;

		$criteria->compare('ownerClass', $this->ownerClass, true);
		$criteria->compare('ownerId', $this->ownerId);
		$criteria->compare('name', $this->name, true);
		$criteria->compare('code', $this->code, true);
		$criteria->compare('validFrom', $this->validFrom, true);
		$criteria->compare('validUntil', $this->validUntil, true);

		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
			));
	}

	public function behaviors() {
		return array(
			'owner'=>array(
				'class'=>'application.behaviors.DynamicBelongsToBehavior',
				'classField'=>'ownerClass',
				'foreignKeyField'=>'ownerId',
			),
			'nameBehavior'=>array(
				'class'=>'application.behaviors.InsuranceListBehavior',
				'attribute'=>'name',
			),
		);
	}

	public function getMenu($action, $toolbar = false) {
		$name = __CLASS__;
		$user = Yii::app()->user;
		$tool = Yii::app()->request->baseUrl . '/images/toolbars';
		$menu = array();

		$label = "View $name";
		if ($toolbar) $label = "<img src=\"$tool/magnifier-left.png\" />";
		$menu[] = array(
			'label'=>$label,
			'url'=>array('view', 'id'=>$this->id),
			'visible'=>$action != 'view' && $user->checkAccess("$name.View"),
			'linkOptions'=>array('title'=>'View'),
		);

		$label = "Update $name";
		if ($toolbar) $label = "<img src=\"$tool/pencil.png\" />";
		$menu[] = array(
			'label'=>$label,
			'url'=>array('update', 'id'=>$this->id),
			'visible'=>$action != 'update' &&
			$user->checkAccess("$name.Update"),
			'linkOptions'=>array('title'=>'Update'),
		);

		$label = "Delete $name";
		if ($toolbar) $label = "<img src=\"$tool/cross-script.png\" />";
		$menu[] = array(
			'label'=>$label,
			'url'=>'#',
			'visible'=>$user->checkAccess("$name.Delete"),
			'linkOptions'=>array(
				'submit'=>array('delete', 'id'=>$this->id),
				'confirm'=>'Are you sure you want to delete this item?',
				'title'=>'Delete',
			)
		);
		return $menu;
	}

}